import java.io.File;

public class MavanRepositoryCleaner {

	public static void main(String[] args) {
		String userHome = System.getProperty("user.home");
//		String userHome = System.getProperties().getProperty("user.home");
		String mavenHome = userHome + "\\.m2\\repository";
		if (args.length > 0) {
			mavenHome = args[0];
		}

		File file = new File(mavenHome);
		System.out.println("maven home: " + mavenHome);
		lookUp(file);
	}

	public static void lookUp(File file) {

		if (file.isDirectory()) {
			File[] list = file.listFiles();
			if (isAllFile(list) && !hasJarFile(list)) {
				System.out.println("find not jar dir: "
						+ file.getAbsolutePath());
				//deleteFile(file);
			}

			for (File f : list) {
				if (f.isDirectory()) {
					lookUp(f);
				}
			}
		}
	}

	public static boolean isAllFile(File[] list) {
		for (File f : list) {
			if (f.isDirectory()) {
				return false;
			}
		}
		return true;
	}

	public static boolean hasJarFile(File[] list) {
		for (File f : list) {
			if (f.getName().endsWith(".jar")) {
				return true;
			}
		}
		return false;
	}

	public static void deleteFile(File oldPath) {
		if (oldPath.isDirectory()) {
			System.out.println("Start clean path " + oldPath);
			File[] files = oldPath.listFiles();
			for (File file : files) {
				deleteFile(file);
			}
			oldPath.delete();
		} else {
			oldPath.delete();
		}
	}
}
